package com.zzwtec.information.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.zzwtec.information.dao.BaseDao;
import com.zzwtec.interfaces.Page;
import com.zzwtec.information.model.Audit;

@Repository(value = "auditDao")
public class AuditDao extends BaseDao<Audit>{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	private final String TABLE_NAME = "audit";
	
	
	/**
	 * 分页查询
	 * @param page
	 * @return 数据模型列表
	 */
	@Transactional(readOnly = true)
	public Page<Audit> selectPage(Page<Audit> page) {
		return super.selectPage(Audit.class, TABLE_NAME, page, new AuditRowMapper());
	}
	
	/**
	 * 全查询，一般不推荐使用，除非数据量不大
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Audit> selectAll() {		
		return super.selectAll(TABLE_NAME, new AuditRowMapper());
	}

	/**
	 * 获取一条记录
	 * 
	 * @param id
	 * @return
	 */
	@Transactional(readOnly = true)
	public Audit selectOne(String id) {		
		return super.selectOne(TABLE_NAME, id, new AuditRowMapper());

	}

	

	

	/**
	 * 更新单表数据
	 * 
	 * @param sql
	 * @return
	 */
	public int update(String sql) {
		return jdbcTemplate.update(sql);
	}

	/**
	 * 批量更新单表数据
	 * 
	 * @param sql
	 * @param batchArgs
	 * @return
	 */
	public int[] batchUpdate(String sql) {
		return jdbcTemplate.batchUpdate(sql);
	}

	/**
	 * 更新区域
	 * @param bean
	 * @return
	 */
	public int update(Audit bean) {
		String sql = "update audit set type= ?,flow_id= ?,applicant= ?,applicant_time= ?,auditor= ?,auditor_time= ?,agree= ?,opinion= ?,next= ?,finish= ? where id= ?"; 
		Object[] params = new Object[]{bean.getType(),bean.getFlowId(),bean.getApplicant(),bean.getApplicantTime(),bean.getAuditor(),bean.getAuditorTime(),bean.isAgree(),bean.getOpinion(),bean.getNext(),bean.isFinish(),bean.getId()};;
		return update(sql, params);
	}
	
	/**
	 * 更新单表数据
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	public int update(String sql, Object... params) {
		return jdbcTemplate.update(sql, params);
	}

	/**
	 * 批量更新单表数据,支持sql预处理语句参数
	 * 
	 * @param sql
	 * @param batchArgs
	 * @return
	 */
	public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
		return jdbcTemplate.batchUpdate(sql, batchArgs);
	}

	/**
	 * 添加区域
	 * @param bean
	 * @return
	 */
	public int insert(Audit bean) {
		String sql = "insert into audit (id,type,flow_id,applicant,applicant_time,auditor,auditor_time,agree,opinion,next,finish) values (?,?,?,?,?,?,?,?,?,?,?)";
		Object[] params = new Object[]{bean.getId(),bean.getType(),bean.getFlowId(),bean.getApplicant(),bean.getApplicantTime(),bean.getAuditor(),bean.getAuditorTime(),bean.isAgree(),bean.getOpinion(),bean.getNext(),bean.isFinish()};;		
		return insert(sql, params);
	}
	
	

	/**
     * 按id删除数据
     * @param id
     * @return
     */
	public int deleteById(String id) {
		String sql = "delete from audit where id = ?";		
		Object[] params = new Object[]{id};
		return delete(sql, params);
	}
	
	/**
	 * 按ids删除数据
     * @param ids
     * @return 受影响行数
	 */
	public int deleteByIds(String[] ids) {
		return super.deleteByIds(TABLE_NAME, ids);
	}
	
	/**
	 * 删除数据 
	 * @param sql
	 * @param params
	 * @return
	 */
	private int delete(String sql, Object... params) {
		if (sql == null || !sql.startsWith("delete")) {
			throw new RuntimeException("不是合法的删除数据sql语句: " + sql);
		}
		return jdbcTemplate.update(sql, params);
	}

}

class AuditRowMapper implements RowMapper<Audit> {
	@Override
	public Audit mapRow(ResultSet rs, int rowNum) throws SQLException {
		Audit bean = new Audit();		
		bean.setId(rs.getString("id"));
		bean.setType(rs.getInt("type"));
		bean.setFlowId(rs.getString("flow_id"));
		bean.setApplicant(rs.getString("applicant"));
		bean.setApplicantTime(rs.getLong("applicant_time"));
		bean.setAuditor(rs.getString("auditor"));
		bean.setAuditorTime(rs.getLong("auditor_time"));
		bean.setAgree(rs.getBoolean("agree"));
		bean.setOpinion(rs.getString("opinion"));
		bean.setNext(rs.getString("next"));
		bean.setFinish(rs.getBoolean("finish"));
		return bean;
	}

}